/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 日期视图
    /// </summary>
    public class FCDateTimePicker : FCTextBox {
        /// <summary>
        /// 创建日期视图
        /// </summary>
        public FCDateTimePicker() {
        }

        public FCCalendar m_calendar;

        /// <summary>
        /// 获取日历视图
        /// </summary>
        public virtual FCCalendar getCalendar() {
            return m_calendar;
        }

        /// <summary>
        /// 设置日历视图
        /// </summary>
        public virtual void setCalendar(FCCalendar value) {
            m_calendar = value;
        }

        public String m_customFormat = "yyyy-MM-dd";

        /// <summary>
        /// 获取日期格式
        /// </summary>
        public virtual String getCustomFormat() {
            return m_customFormat;
        }

        /// <summary>
        /// 设置日期格式
        /// </summary>
        public virtual void setCustomFormat(String value) {
            m_customFormat = value;
        }

        public FCButton m_dropDownButton;

        /// <summary>
        /// 获取下拉按钮
        /// </summary>
        public virtual FCButton getDropDownButton() {
            return m_dropDownButton;
        }

        public FCMenu m_dropDownMenu;

        /// <summary>
        /// 获取下拉菜单
        /// </summary>
        public virtual FCMenu getDropDownMenu() {
            return m_dropDownMenu;
        }

        public bool m_showTime = true;

        /// <summary>
        /// 获取是否显示时间
        /// </summary>
        public virtual bool showTime() {
            return m_showTime;
        }

        /// <summary>
        /// 设置是否显示时间
        /// </summary>
        public virtual void setShowTime(bool value) {
            m_showTime = value;
        }

        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="invoke"></param>
        public override void callEvent(String eventName, Object sender, Object invoke) {
            base.callEvent(eventName, sender, invoke);
            if (eventName == "onselectedtimechanged") {
                onSelectedTimeChanged();
            }
        }

        /// <summary>
        /// 调用触摸方法
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="touchInfo"></param>
        /// <param name="invoke"></param>
        public override void callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke) {
            base.callTouchEvent(eventName, sender, touchInfo, invoke);
            if (eventName == "ontouchdown" && sender == m_dropDownButton) {
                onDropDownOpening();
            }
        }

        /// <summary>
        /// 创建日历
        /// </summary>
        /// <returns></returns>
        public virtual FCCalendar createCalendar() {
            FCCalendar calendar = new FCCalendar();
            calendar.setDock(FCDockStyle.Fill);
            return calendar;
        }

        /// <summary>
        /// 销毁方法
        /// </summary>
        public override void delete() {
            if (!isDeleted()) {
                if (m_calendar != null) {
                    m_calendar.removeEvent(this, "onselectedtimechanged");
                }
                if (m_dropDownButton != null) {
                    m_dropDownButton.removeEvent(this, "ontouchdown");
                }
                if (m_dropDownMenu != null) {
                    getNative().removeView(m_dropDownMenu);
                    m_dropDownMenu.delete();
                    m_dropDownMenu = null;
                }
            }
            base.delete();
        }

        /// <summary>
        /// 下拉按钮的点击方法
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <param name="touchInfo">触摸信息</param>
        private void dropDownButtonTouchDown(Object sender, FCTouchInfo touchInfo, Object invoke) {
            onDropDownOpening();
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "DateTimePicker";
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        /// <param name="type">类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            if (name == "customformat") {
                type = "text";
                value = getCustomFormat();
            } else if (name == "showtime") {
                type = "bool";
                value = FCTran.boolToStr(showTime());
            } else {
                base.getAttribute(name, ref value, ref type);
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.add("CustomFormat");
            attributeNames.add("ShowTime");
            return attributeNames;
        }

        /// <summary>
        /// 下拉菜单显示方法
        /// </summary>
        public virtual void onDropDownOpening() {
            m_calendar.setSize(m_dropDownMenu.getSize());
            if (m_calendar != null && !m_showTime) {
                m_calendar.getTimeDiv().setHeight(0);
            }
            m_dropDownMenu.setNative(getNative());
            FCPoint nativePoint = pointToNative(new FCPoint(0, getHeight()));
            m_dropDownMenu.setLocation(nativePoint);
            m_dropDownMenu.setVisible(true);
            if (m_calendar != null) {
                m_calendar.setMode(FCCalendarMode.Day);
            }
            m_dropDownMenu.bringToFront();
            m_dropDownMenu.invalidate();
        }

        /// <summary>
        /// 添加视图方法
        /// </summary>
        public override void onLoad() {
            base.onLoad();
            FCHost host = getNative().getHost();
            if (m_dropDownButton == null) { 
                m_dropDownButton = host.createInternalView(this, "dropdownbutton") as FCButton;
                addView(m_dropDownButton);
                m_dropDownButton.addEvent(this, "ontouchdown", null);
            }
            //创建下拉菜单及日历
            if (m_dropDownMenu == null)
            {
                m_dropDownMenu = host.createInternalView(this, "dropdownmenu") as FCMenu;
                m_dropDownMenu.setShowHScrollBar(false);
                m_dropDownMenu.setShowVScrollBar(false);
                getNative().addView(m_dropDownMenu);
                if (m_calendar == null)
                {
                    m_calendar = createCalendar();
                    m_dropDownMenu.addView(m_calendar);
                    m_calendar.addEvent(this, "onselectedtimechanged", null);
                }
            }
            if (m_dropDownMenu != null) {
                m_dropDownMenu.setNative(getNative());
            }
        }

        /// <summary>
        /// 选中时间改变时触发
        /// </summary>
        public virtual void onSelectedTimeChanged() {
            callEvents("onselectedtimechanged");
            if (m_calendar != null) {
                CDay selectedDay = m_calendar.getSelectedDay();
                if (selectedDay != null) {
                    DateTime date = new DateTime(selectedDay.getYear(), selectedDay.getMonth(), selectedDay.getDay(), m_calendar.getTimeDiv().getHour(),
                        m_calendar.getTimeDiv().getMinute(), m_calendar.getTimeDiv().getSecond());
                    setText(date.ToString(m_customFormat));
                    invalidate();
                }
            }
        }

        /// <summary>
        /// 选中日期改变事件回调方法
        /// </summary>
        /// <param name="sender">发送者</param>
        private void selectedTimeChanged(Object sender, Object invoke) {
            onSelectedTimeChanged();
        }

        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value) {
            if (name == "customformat") {
                setCustomFormat(value);
            } else if (name == "showtime") {
                setShowTime(FCTran.strToBool(value));
            } else {
                base.setAttribute(name, value);
            }
        }

        /// <summary>
        /// 更新布局方法
        /// </summary>
        public override void update() {
            if (m_native != null && !m_native.m_lockUpdate)
            {
                base.update();
                int width = getWidth(), height = getHeight();
                if (m_dropDownButton != null)
                {
                    int dWidth = m_dropDownButton.getWidth();
                    setPadding(new FCPadding(0, 0, dWidth, 0));
                    m_dropDownButton.setLocation(new FCPoint(width - dWidth, 0));
                    m_dropDownButton.setSize(new FCSize(dWidth, height));
                }
            }
        }
    }
}
